package org.infosys.org.controller;

import org.infosys.basic.utils.AjaxResult;
import org.infosys.basic.utils.PageList;
import org.infosys.org.domain.Department;
import org.infosys.org.domain.Employee;
import org.infosys.org.query.EmployeeQuery;
import org.infosys.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadAll
    * @Description: 查询所有员工信息
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 12:38
    * @Parameters: []
    * @Return java.util.List<org.infosys.org.domain.Employee>
    */
    @GetMapping
    public List<Employee> loadAll() {
        return employeeService.loadAll();
    }

    /**
    * @Title: loadById
    * @Description: 根据id查询员工
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 21:27
    * @Parameters: [id]
    * @Return org.infosys.org.domain.Employee
    */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除员工
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 21:36
    * @Parameters: [id]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后再试!").setSuccess(false);
        }
    }

    /**
    * @Title: addOrUpt
    * @Description: 修改和新增
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 22:20
    * @Parameters: [employee]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee) {
        try {
            if (ObjectUtils.isEmpty(employee.getId())) {
                employeeService.save(employee);
                System.out.println(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙,请稍后再试!").setSuccess(false);
        }
    }

    /**
    * @Title: queryEmployee
    * @Description: 条件查询
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 23:02
    * @Parameters: [employeeQuery]
    * @Return org.infosys.basic.utils.PageList<org.infosys.org.domain.Employee>
    */
    @PostMapping
    public PageList<Employee> queryEmployee(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.loadQuery(employeeQuery);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试!").setSuccess(false);
        }
    }
}


